<template>
  <!-- 交易平台 新增 搜索 -->
  <div>
    <!-- 表单 -->
    <TopFilter ref="form" :cols="3" :list="list" labelWidth="100" :collapse="false" @filterChange="formChange"></TopFilter>
    <!-- 表格 -->
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partPartInventoryPartInventoryComponentsJiaoyipingtai"
      :fetchapi="fetchApi"
      :params="fetchParams"
      height="200"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
      :onPageChange="onPageChange"
      :onEnterEvent="onEnterEvent"
      :onSyncTableData="handleTableDataChange"
    ></FilterTable>
    <!-- selectionType="single"   多选-->
    <div class="btnWrap">
      <el-button @click="$emit('cancel')">取 消</el-button>
      <el-button type="primary" @click="confirmBtnClick">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { state, mutations } from './../store';
import { queryPartInfo, queryPartStock } from '@/api/part/partOutbound/repairMaterials/index.js';
import { partgetfourData, queryPlatForm } from '@/api/part/partInventory/partInventory/index.js';
import { notifyAction } from '@/utils';

export default {
  name: 'jiaoyipingtai',
  props: ['orderType', 'orderNo'],
  data() {
    return {
      onChangeColumn: [],
      list: this.createList(),
      columns: this.createColumns(),
      fetchApi: queryPlatForm,
      fetchParams: {
        orderNo: this.orderNo,
        orderType: this.orderType,
        xhrAbort: false
      },
      tableDataList: []
    };
  },
  mounted() {
    this.list.find(x => x.fieldName === 'orderNo').initialValue = this.orderNo;
  },
  methods: {
    createList() {
      return [
        {
          type: 'INPUT',
          label: '平台订单编号',
          fieldName: 'orderNo'
        }
      ];
    },
    createColumns() {
      return [
        {
          title: '序号',
          sorter: true,
          width: 70,
          dataIndex: 'xuhao',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '平台订单编号',
          // sorter: true,
          dataIndex: 'ORDER_NO',
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单日期',
          // sorter: true,
          dataIndex: 'ORDER_DATE',
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '配件代码',
          // sorter: true,
          dataIndex: 'PART_NO',
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          // sorter: true,
          dataIndex: 'PART_NAME',
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          // sorter: true,
          width: 150,
          showOverflowTooltip: true,
          dataIndex: 'S_OWNER_NAME',
          filter: true,
          filterType: 'input'
        },
        {
          title: '数量',
          // sorter: true,
          dataIndex: 'QUANTITY',
          filter: true,
          filterType: 'number'
        },
        {
          title: '可入库数量',
          dataIndex: 'IN_QUANTITY',
          filter: true,
          filterType: 'number'
        },
        {
          title: '含税单价',
          // sorter: true,
          dataIndex: 'IN_PRICE_TAXED',
          filter: true,
          filterType: 'number'
        },
        {
          title: '已入库数量',
          // sorter: true,
          dataIndex: 'PT_QUANTITY',
          filter: true,
          filterType: 'number'
        },
        {
          title: '物流名称',
          // sorter: true,
          dataIndex: 'LOGISTICS',
          filter: true,
          filterType: 'input'
        },
        {
          title: '物流单号',
          // sorter: true,
          dataIndex: 'DELIVERY_ORDER_NO',
          filter: true,
          filterType: 'input'
        }
      ];
    },
    handleTableDataChange(e) {
      this.tableDataList = e;
      e.map(item => {
        item.IN_QUANTITY = (item.QUANTITY || 0) - (item.PT_QUANTITY || 0);
      });
    },
    // 获取表单数据
    //  async partgetfourData () {
    //   let params = {}
    //   params.orderType  = this.orderType
    //   params.orderNo = ''
    //   const res = await partgetfourData (params)
    //   // res.data.rows
    // },
    formChange(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, val, { xhrAbort: false });
    },
    onPageChange() {
      console.log('onPageChange');
    },
    // 搜索
    FormPanelSeacher() {
      this.$refs.form.SUBMIT_FORM();
    },
    async onEnterEvent(row) {
      // 双击选中  整合配件信息
      console.log('onEnterEvent', row);
      this.$emit('dblclickPartInfo', Object.assign({}, row));
      return;
      const res = await queryPartStock({
        partNo: row['PART_NO'],
        storageCode: row['STORAGE_CODE']
      });
      if (res.resultCode != 200) {
        console.log('查询配件 失败', res);
        return;
      }
      console.log('查询配件 成功', res);
      const part = res.data.rows[0];
      // 整合配件信息
      // const partInfo = Object.assign({}, row, part);
      const partInfo = Object.assign({}, part);
      console.log('整合后的配件信息', partInfo);
      this.$parent.setPartInfo(partInfo);
    },
    // 确定
    confirmBtnClick() {
      let flag = true;
      let checkFalseList = [];
      this.onChangeColumn.map(item => {
        item.LIMIT_QUANTITY = item.IN_QUANTITY;
        if (item.IN_QUANTITY <= 0) {
          flag = false;
          checkFalseList.push(item.$index + 1);
        }
      });
      if (!flag) {
        notifyAction('可入库数量小于0不能入库，行项：' + checkFalseList.join(','), 'warning');
        return;
      }
      this.$emit('confirm', this.onChangeColumn);
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
